

package traffic.light.simulator.town.trafficLight;

import java.awt.Color;
import java.awt.Graphics;
import traffic.light.simulator.town.unity.CrossRoadType;
import traffic.light.simulator.util.IDrawable;
import traffic.light.simulator.town.unity.TownElement;
import traffic.light.simulator.town.unity.TownUnity;

/**
 *
 * first abstract class representing the Trafficlight
 */
public abstract class TrafficLight implements IDrawable {

    protected boolean up=true;
    protected boolean right=false;
    protected boolean left=false;
    protected boolean down=true;
    protected int xTU;
    protected int yTU;
    protected CrossRoadType type;

    public TrafficLight(int xTU, int yTU,CrossRoadType type) {
        super();
        this.xTU=xTU;
        this.yTU=yTU;
        this.type=type;

        switch(this.type){
            case TYPE_3:
                this.right=true;
                this.left=true;
                break;
            case TYPE_4:
                this.up=true;
                this.down=true;
        }
    }

    /**
     * change the color of Trafficlights
     */
    public void change(){
        this.up=!this.up;
        this.right=!this.right;
        this.down=!this.down;
        this.left=!this.left;
    }
    /**
     * put all the Trafficlights in green
     * use for one way road with pedestrian trafficlight
     * @param greenorred
     */
    public void changeAllIn(boolean greenorred){
        this.up=greenorred;
        this.right=greenorred;
        this.down=greenorred;
        this.left=greenorred;
    }

    @Override
    public void draw(Graphics g) {
        int TLSize=(int)(((float)1/4)*TownElement.getSize());
        int x=xTU*TownUnity.getSize().width;
        int y=yTU*TownUnity.getSize().height;

        //true=green && false=red
        Color colorUP=Color.RED;
        Color colorRight=Color.RED;
        Color colorDown=Color.RED;
        Color colorLeft=Color.RED;

        if(this.up)
            colorUP=Color.GREEN;
        if(this.right)
            colorRight=Color.GREEN;
        if(this.down)
            colorDown=Color.GREEN;
        if(this.left)
            colorLeft=Color.GREEN;

        switch(this.type){
            
            case TYPE_0:
                g.setColor(new Color(210, 210, 210));
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),2*TownElement.getSize(), TLSize);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());


                g.setColor(colorUP);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorRight);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorLeft);
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                break;
            case TYPE_1:
                g.setColor(new Color(210, 210, 210));
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),2*TownElement.getSize(), TLSize);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,2*TownElement.getSize(), TLSize);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());

                g.setColor(colorUP);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorRight);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorDown);
                g.fillRect(x+4*TownElement.getSize()-TLSize,y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                g.setColor(colorLeft);
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                break;
            case TYPE_2:
                g.setColor(new Color(210, 210, 210));
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,2*TownElement.getSize(), TLSize);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());

                g.setColor(colorRight);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorDown);
                g.fillRect(x+4*TownElement.getSize()-TLSize,y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                g.setColor(colorLeft);
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                break;
            case TYPE_3:
                g.setColor(new Color(210, 210, 210));
                g.fillRect(x+3*TownElement.getSize()-TLSize, y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());


                g.setColor(colorRight);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(),TLSize,TLSize);//                g.setColor(Color.GREEN);
                g.setColor(colorLeft);
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                break;
             case TYPE_4:
                g.setColor(new Color(210, 210, 210));
                g.fillRect(x+2*TownElement.getSize(), y+3*TownElement.getSize(),2*TownElement.getSize(), TLSize);

                g.setColor(colorUP);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorDown);
                g.fillRect(x+4*TownElement.getSize()-TLSize,y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                break;
            case TYPE_5:
                g.setColor(new Color(210, 210, 210));
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),2*TownElement.getSize(), TLSize);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,2*TownElement.getSize(), TLSize);
                
                g.setColor(colorUP);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorRight);
                g.fillRect(x+4*TownElement.getSize()-TLSize, y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorDown);
                g.fillRect(x+4*TownElement.getSize()-TLSize,y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                break;
            case TYPE_6:
                g.setColor(new Color(210, 210, 210));
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),2*TownElement.getSize(), TLSize);
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,2*TownElement.getSize(), TLSize);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(), TLSize,2*TownElement.getSize());

                g.setColor(colorUP);
                g.fillRect(x+2*TownElement.getSize(), y+2*TownElement.getSize(),TLSize,TLSize);
                g.setColor(colorDown);
                g.fillRect(x+4*TownElement.getSize()-TLSize,y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                g.setColor(colorLeft);
                g.fillRect(x+2*TownElement.getSize(),y+4*TownElement.getSize()-TLSize,TLSize,TLSize);
                break;
        }
    }

    @Override
    public void draw(Graphics g, boolean editionMode) {
        this.draw(g);
    }

    /**
     * Get the value of Trafficlight from a position in the map
     * return true if Trafficlight is green or without Trafficlight
     * @param xRE
     * @param yRE
     * @return
     */
    public boolean getTLValueFromPosition(int xRE, int yRE){
        int x=xTU*TownUnity.getSize().width;
        int y=yTU*TownUnity.getSize().height;

        if(x+2*TownElement.getSize()==xRE && y+1*TownElement.getSize()==yRE)
            return this.up;
        if(x+4*TownElement.getSize()==xRE && y+2*TownElement.getSize()==yRE)
            return this.right;
        if(x+3*TownElement.getSize()==xRE && y+4*TownElement.getSize()==yRE)
            return this.down;
        if(x+1*TownElement.getSize()==xRE && y+3*TownElement.getSize()==yRE)
            return this.left;
        return true;
    }

}
